package gof.com.yew.prototype;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

/**
 * 深拷贝
 * @author yw
 */

public class PrototypeDemo03 {
	
	public static void main(String[] args) throws Exception{
		Address address = new Address("中国", "湖北"); 
		Sheep2 sheep = new Sheep2("多利", address);
		
		//使用系列化和反序列化实现深拷贝
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		ObjectOutputStream oos = new ObjectOutputStream(bos);
		oos.writeObject(sheep);
		byte[] bs = bos.toByteArray();
		
		ByteArrayInputStream bis = new ByteArrayInputStream(bs);
		ObjectInputStream ois = new ObjectInputStream(bis);
		Sheep2 sheep1  = (Sheep2)ois.readObject();
		
		System.out.println(sheep.getName());
		System.out.println(sheep.getAddress().getCuntry());
		System.out.println(sheep.getAddress().getLoc());
		System.out.println("-------------------------------------------------");
		System.out.println(sheep1.getName());
		System.out.println(sheep1.getAddress().getCuntry());
		System.out.println(sheep1.getAddress().getLoc());
		System.out.println("-------------------------------------------------");
		address.setLoc("浙江");
		System.out.println(sheep.getAddress().getLoc());
		System.out.println(sheep1.getAddress().getLoc());
	}

}
